package com.school.app;

import able.stripes.JSP;
import com.google.inject.Inject;
import com.school.app.dao.PenggunaDAO;
import com.school.app.manager.UserManager;
import com.school.app.model.Pengguna;
import com.wideplay.warp.persist.Transactional;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationMethod;

@UrlBinding("/login")
//@AuthRequired
public class LoginActionBean extends WelcomeActionBean {

   
    @Validate(required=true)
    private String userLogin;
    @Validate(required=true)
    private String userPassword;

    @Inject PenggunaDAO penggunaDAO;
    @Inject UserManager userManager;
    

    @DefaultHandler
    @DontValidate
    public Resolution showForm() {
        return new JSP("login.jsp");
    } 
    
    public Resolution login() {

        System.out.println("----login----");
        Pengguna pengguna = penggunaDAO.findById(getUserLogin());
        getContext().setCurrentUser(pengguna);  
//        pengguna.setTarikhAkhirLogin(new Date());
        userManager.updatePengguna(pengguna);
        
        return new RedirectResolution(WelcomeActionBean.class);        
    }

    @ValidationMethod
    @Transactional
    public void validate_login() {    
        if (!userManager.authenticate(getUserLogin(), getUserPassword())) {
            addError("user.userId", "user", "badLogin");
        }        
    }

    public String getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

}
